package _interview100;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 20. 有效的括号
 */
public class No20 {
    public boolean isValid(String s) {
        Deque<Character> stack = new LinkedList<>();
        for (char c : s.toCharArray()) {
            if (c == '{' || c == '[' || c == '(') stack.push(c);
            else {
                if (stack.isEmpty()) return false;
                else if (c == '}' && stack.pop() != '{') return false;
                else if (c == ']' && stack.pop() != '[') return false;
                else if (c == ')' && stack.pop() != '(') return false;
            }
        }
        return stack.isEmpty();
    }
}
